package com.cskaoyan.order.mq.provider;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.client.exception.MQBrokerException;
//import org.apache.rocketmq.client.exception.MQClientException;
//import org.apache.rocketmq.client.producer.DefaultMQProducer;
//import org.apache.rocketmq.client.producer.SendResult;
//import org.apache.rocketmq.client.producer.SendStatus;
//import org.apache.rocketmq.common.message.Message;
//import org.apache.rocketmq.remoting.exception.RemotingException;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import java.nio.charset.Charset;
//
///**
// * @Aurhor: 帅的一批
// * @CreateAt: 2022/10/28 22:22
// * @Description:
// * @PACKAGE_NAME： com.cskaoyan.order.mq.provider
// */
//
//@Component
//@Slf4j
//public class DelayCancelProvider {
//
//    private DefaultMQProducer producer;
//
//    @PostConstruct
//    private void init(){
//        producer = new DefaultMQProducer("delay_order_cancel_producer");
//        producer.setNamesrvAddr("127.0.0.1:9999");
//
//        try {
//            producer.start();
//        } catch (MQClientException e) {
//            log.info("订单提供者启动失败");
//            e.printStackTrace();
//        }
//    }
//
//    public boolean sendDelayMessage(String orderId){
//
//        Message message = new Message();
//        message.setTopic("order_delay_cancel");
//        message.setBody(orderId.getBytes(Charset.forName("utf-8")));
//
//        SendResult send = null;
//
//        try {
//            send = producer.send(message);
//        } catch (MQClientException e) {
//            e.printStackTrace();
//        } catch (RemotingException e) {
//            e.printStackTrace();
//        } catch (MQBrokerException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        log.info("post send msg {}-{}", orderId, send.getSendStatus());
//        if (send != null && SendStatus.SEND_OK.equals(send.getSendStatus())) {
//            // 发送成功
//            log.info("send message success {} ", orderId);
//            return true;
//        }
//
//        log.info("send message fail {} ", orderId);
//        // 消息发送失败
//        return false;
//    }
//
//}


import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.Charset;

@Component
@Slf4j
public class DelayCancelProvider {

    private DefaultMQProducer producer;

    @Value("${rocketmq.address}")
    String rocketmqAddress;

    @PostConstruct
    public void init() {
        // 初始化producer对象
        producer = new DefaultMQProducer("delay_order_cancel_producer");

        // 设置nameserver地址
        producer.setNamesrvAddr(rocketmqAddress);

        // 启动producer
        try {
            producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }



    public boolean sendDelayMessage(String orderId) {
        // 1. 准备待发送的消息
        Message message = new Message();
        message.setTopic("order_delay_cancel");
        message.setBody(orderId.getBytes(Charset.forName("utf-8")));
        //延迟半小时
        message.setDelayTimeLevel(4);
        // 2. 发送消息
        SendResult send = null;
        System.out.println(rocketmqAddress);
        try {
            send = producer.send(message);
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("post send msg {}-{}", orderId, send.getSendStatus());
        if (send != null && SendStatus.SEND_OK.equals(send.getSendStatus())) {
            // 发送成功
            return true;
        }

        // 消息发送失败
        return false;
    }
}
